﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using celinda.Repository;
using celinda.Class;

namespace celinda.Formulaire.Tiers
{
    public partial class SocieteForm : BaseFormSplit
    {
        private SocieteRepositoryNH repS;
        public SocieteForm()
        {
            repS = new SocieteRepositoryNH();
            InitializeComponent();
            ChargeForm();
        }

        private void ChargeForm(Int32 Id = 0)
        {
            kDg.DataSource = repS.GetAll();
            kDg.Columns["Id"].Visible = false;
            kDg.Columns["Utilisateurs"].Visible = false;
            kDg.Columns["Sites"].Visible = false;
            kDg.Columns["Fournisseurs"].Visible = false;


            tssNbLigneGrid.Text = kDg.Rows.Count.ToString();
            if (kDg.Rows.Count == 0)
                tssMode.Text = Convert.ToString(Class.Const.ModeCreation);
            else
            {
                tssMode.Text = Convert.ToString(Class.Const.ModeModification);
                if (Id != 0)
                {
                    foreach (DataGridViewRow row in kDg.Rows)
                    {
                        if ((Int32)row.Cells["Id"].Value == Id)
                        {
                            row.Selected = true;
                            break;
                        }
                    }
                }
                else
                {
                    kDg.Rows[0].Selected = true;
                }
            }
        }

        protected override void Ajout()
        {
            base.Ajout();

            kTxtNom.Text = String.Empty;
            kTxtAdresse.Text = String.Empty;
            kTxtCP.Text = String.Empty;
            kTxtVille.Text = String.Empty;
            kTxtPays.Text = String.Empty;
            kTxtSiret.Text = String.Empty;
            kTxtRaisonSocial.Text = String.Empty;
            kNUPCapitalSocial.Value = new Decimal(100);

            tssMode.Text = Convert.ToString(Class.Const.ModeCreation);
        }

        protected override void Valide()
        {
            base.Valide();
            Societe s = new Societe();
            if (tssMode.Text.Equals(Class.Const.ModeCreation.ToString()))
            {
                s.Nom = kTxtNom.Text;
                s.Adresse = kTxtAdresse.Text;
                s.CodePostal = kTxtCP.Text;
                s.Ville = kTxtVille.Text;
                s.Pays = kTxtPays.Text;
                s.Siret = kTxtSiret.Text;
                s.RaisonSocial = kTxtRaisonSocial.Text;
                s.CapitalSocial = Double.Parse(kNUPCapitalSocial.Value.ToString());

                repS.Save(s);
            }
            else
            {
                s = repS.GetByID(Int32.Parse(kDg.SelectedRows[0].Cells["Id"].Value.ToString()));
                s.Nom = kTxtNom.Text;
                s.Adresse = kTxtAdresse.Text;
                s.CodePostal = kTxtCP.Text;
                s.Ville = kTxtVille.Text;
                s.Pays = kTxtPays.Text;
                s.Siret = kTxtSiret.Text;
                s.RaisonSocial = kTxtRaisonSocial.Text;
                s.CapitalSocial = Double.Parse(kNUPCapitalSocial.Value.ToString());

                repS.Update(s);
            }

            ChargeForm(s.Id);
        }

        protected override void Supprime()
        {
            base.Supprime();

            if(kDg.SelectedRows.Count != 0)
                if (Class.Outils.ChoixOuiNon("Etes-vous sur de vouloir supprimer la Société <" + kDg.SelectedRows[0].Cells["Nom"].Value + "> ?") == System.Windows.Forms.DialogResult.Yes)
                    repS.DeleteByID("Id", Int32.Parse(kDg.SelectedRows[0].Cells["Id"].Value.ToString()));

            ChargeForm();
        }

        protected override void Dupplique()
        {
            base.Dupplique();

            Societe s = repS.GetByID(Int32.Parse(kDg.SelectedRows[0].Cells["Id"].Value.ToString()));
            s.Id = 0;
            s.Sites = new List<Site>();
            s.Utilisateurs = new List<Utilisateur>();
            s.Fournisseurs = new List<Fournisseur>();

            repS.Save(s);

            ChargeForm(s.Id);
        }

        protected override void ChangeEtat()
        {
            base.ChangeEtat();

            Societe s = repS.GetByID(Int32.Parse(kDg.SelectedRows[0].Cells["Id"].Value.ToString()));
            if (s.Etat.Equals(Class.Const.EtatActif))
                s.Etat = Class.Const.EtatInactif;
            else
                s.Etat = Class.Const.EtatActif;

            repS.Update(s);

            ChargeForm(s.Id);
        }

        protected override void kDgRowEnter()
        {
            base.kDgRowEnter();
            if (kDg.SelectedRows.Count > 0)
            {
                Societe s = repS.GetByID(Int32.Parse(kDg.SelectedRows[0].Cells["Id"].Value.ToString()));
                kTxtNom.Text = s.Nom;
                kTxtAdresse.Text = s.Adresse;
                kTxtCP.Text = s.CodePostal;
                kTxtVille.Text = s.Ville;
                kTxtPays.Text = s.Pays;
                kTxtSiret.Text = s.Siret;
                kTxtRaisonSocial.Text = s.RaisonSocial;
                kNUPCapitalSocial.Value = Convert.ToDecimal(s.CapitalSocial);
                kLblDateCreation.Text = s.DateCreation.ToString();
                kLblDateModification.Text = s.DateModification.ToString();

                tssMode.Text = Const.ModeModification.ToString();
            }
        }

        protected override void TssModeTextChanged()
        {
            kTxtNom.Focus();
            base.TssModeTextChanged();

            if (tssMode.Text.Equals(Class.Const.ModeCreation.ToString()))
            {
                kBtnSite.Enabled = false;
                kBtnUtilisateur.Enabled = false;
                kBtnFournisseur.Enabled = false;

            }
            else
            {
                kBtnSite.Enabled = true;
                kBtnUtilisateur.Enabled = true;
                kBtnFournisseur.Enabled = true;
            }
        }
    }
}
